#!/usr/bin/env python
# -*- coding=utf-8 -*-

import sys
reload(sys)
sys.setdefaultencoding('utf8')

# pip install ruamel.yaml

from ruamel import yaml

import rospy
import os
import time
import numpy as np

from cln_msgs.msg import SensorType,CleanDeviceType
from log_parser import Log_Parser
log = Log_Parser()

class User_Data_Server:
    def __init__(self):
        self.__user_config_dir = rospy.get_param("user_config","/home/aoting/user_config/")

        self.__user_params_dir = os.path.join(self.__user_config_dir,"params/")
        self.__user_params_path = os.path.join(self.__user_config_dir,"params/user_params.yaml")

        self.__depth2laser_multi_params_path = os.path.join(self.__user_config_dir,"params/depth_camera/depth2laser_multi.yaml")    # 多相机参数路径

        self.__robot_type = rospy.get_param("robot_type","SW55")    # 兼容80，5个相机
        if self.__robot_type == "sw80":
            self.__depth2laser_multi_params_path = os.path.join(self.__user_config_dir,"params/depth_camera/sw80/depth2laser_multi.yaml")    # 多相机参数路径

        self.__depth_camera_params_path = os.path.join(self.__user_config_dir,"params/depth_camera/sw80/depth_camera.yaml")    # 多相机参数路径

        self.__cleandevice_with_memory_params_path = os.path.join(self.__user_config_dir,"params/cleandevice_with_memory.yaml")     # 外设记忆功能参数文件
        # 传感器禁用列表
        self.__sensor_disable_info_path = os.path.join(self.__user_config_dir,"params/sensors_disable_info.yaml")
        
        self.__sensor_info_path = os.path.join(self.__user_config_dir,"params/sensors_info.yaml")
        self.__device_info_path = os.path.join(self.__user_config_dir,"params/devices_info.yaml")
   
        self.__user_data_dir = os.path.join(self.__user_config_dir,"user_data/")
        self.__dustbox_time_path = os.path.join(self.__user_config_dir,"user_data/dustbox_time.npy")
        
        self.__version_info_dir = rospy.get_param("version_info_dir","/home/aoting/")
        self.__version_info_path = os.path.join(self.__version_info_dir,"version.yaml")

        self.__map_rotate_angle_info_path = os.path.join(self.__user_config_dir,"params/map_rotate_angle.yaml") # 地图旋转角度 临时存储验证 20230811-dw.liu

        # 新的外设参数 20230906-dw.liu
        self.__new_cleandevice_path = os.path.join(self.__user_config_dir,"params/new_cleandevice.yaml") 

        # 消杀参数 
        self.__disinfect_config_path = os.path.join(self.__user_config_dir,"params/disinfect_config.yaml") 

        # 机器人支持的新功能参数 20230927-dw.liu
        self.__robot_funtion_path = os.path.join(self.__user_config_dir,"params/robot_function.yaml") 

        # 机器人sn和型号参数 20230927-dw.liu
        self.__robot_sn_path = os.path.join(self.__user_config_dir,"params/robot_sn.yaml") 

        #外设控制类型
        self.__NO_CONTROL_TYPE = 0
        self.__CONTROL_SPPEED_TYPE = 1
        self.__CONTROL_LIFT_TYPE = 2
        self.__CONTROL_OPEN_TYPE = 3
        
        #使用的传感器
        self.__used_sensor_info = []
        self.__used_sensor_set = set()

        #使用的外设
        self.__used_device_info = []
        self.__used_device_set = set()

        self.__CheckUserFile()

        self.LoadUserParams()
        self.ReadSensorInfo()
        self.ReadDeviceInfo()
        
        # rospy.loginfo("[User_Param_Server]user_config: {}".format(self.__user_config_dir))

    def __CheckUserFile(self):
        '''
            参数配置文件检测
        '''
        if not os.path.exists(self.__user_params_dir):
            os.makedirs(self.__user_params_dir)

        if not os.path.exists(self.__user_data_dir):
            os.makedirs(self.__user_data_dir)

        if not os.path.exists(self.__user_params_path):
            # with open(self.__user_params_path,'w') as f:
            #     f.close()
            
            ret = os.system("touch {}".format(self.__user_params_path))
            logname = "User_Data_Server"
            log_msg = "[User_Data_Server][__CheckUserFile]create user_params os_touch_ret:<{}>".format(ret)
            log.LogInfo(logname,log_msg)
        
        if not os.path.exists(self.__sensor_info_path):
            self.WriteDefaultInfo(self.__sensor_info_path,self.GetDefaultSensorInfo())

        if not os.path.exists(self.__device_info_path):
            self.WriteDefaultInfo(self.__device_info_path,self.GetDefaultDeviceInfo())

        # 记忆模式清扫外设默认参数文件，没有则新建 第一次
        # if not os.path.exists(self.__cleandevice_with_memory_params_path):
        #     self.WriteDefaultInfo(self.__cleandevice_with_memory_params_path,self.GetDefaultCleanDeviceWithMemoryInfo())

        # 新记忆模式清扫外设参数文件，没有则从旧的拷贝 第二次修改 202300906-dw.liu
        # if not os.path.exists(self.__new_cleandevice_path):
        #     local_param_data_now = self.ReadCleandeviceWithMemoryParams()
        #     new_cleandevice_param_temp = local_param_data_now   # 新的外设参数 过渡
        #     new_cleandevice_param_temp.pop("default_memory_name")
        #     # print(new_cleandevice_param_temp)
        #     new_cleandevice_param_yaml = {} # 新的外设参数 字典
        #     for keyStr,value in new_cleandevice_param_temp.items():
        #         new_cleandevice_param_yaml[value["item_name"]] = value
        #     self.WriteDefaultInfo(self.__new_cleandevice_path,new_cleandevice_param_yaml)

        # 新记忆模式清扫外设参数文件， 没有则创建默认参数 第三次修改 202301113-dw.liu
        if not os.path.exists(self.__new_cleandevice_path):
            self.WriteDefaultInfo(self.__new_cleandevice_path,self.GetDefaultNewDeviceInfo())

        # 传感器使能失能文件，没有自动创建，默认全部禁止禁用即传感器正常启动
        if not os.path.exists(self.__sensor_disable_info_path):
            self.WriteDefaultInfo(self.__sensor_disable_info_path,self.GetDefaultSensorDisableInfo())

        # 消杀参数文件，没有自动创建，默认。。。
        if not os.path.exists(self.__disinfect_config_path):
            self.WriteDefaultInfo(self.__disinfect_config_path,self.GetDefaultDisinfectConfigInfo())
        # 加载消杀参数
        os.system("rosparam load {}".format(self.__disinfect_config_path))

        # 机器人支持的新功能参数文件，没有自动创建，默认。。。从20230927开始
        if not os.path.exists(self.__robot_funtion_path):
            self.WriteDefaultInfo(self.__robot_funtion_path,self.GetRobotFuntionInfo())


    def LoadUserParams(self):
        '''
            加载参数
        '''
        os.system("rosparam load {}".format(self.__user_params_path))
    
    def ReadUserParams(self):
        '''
            读取参数
        '''
        yaml_data={}
        #print("ReadUserParams1")
        if os.path.exists(self.__user_params_path):
            #print("ReadUserParams")
            with open(self.__user_params_path,'r') as f: 
                yaml_data = yaml.load(f,Loader=yaml.Loader)
        return yaml_data
    
    def ReadDepth2laserMultiParams(self):
        '''
            读取参数
        '''
        yaml_data={}
        if os.path.exists(self.__depth2laser_multi_params_path):
            with open(self.__depth2laser_multi_params_path,'r') as f: 
                yaml_data = yaml.load(f,Loader=yaml.Loader)

        if os.path.exists(self.__depth_camera_params_path):
            with open(self.__depth_camera_params_path,'r') as f: 
                depth_camera_data = yaml.load(f,Loader=yaml.Loader)
                if depth_camera_data:
                    yaml_data.update(depth_camera_data)    

        # print(yaml_data)

        return yaml_data
    
    def WriteUserParams(self,param_data):
        '''
            写入参数
            - param_data (dict)
        '''
        yaml_data = self.ReadUserParams()
        if yaml_data : 
            yaml_data.update(param_data)
        else: 
            yaml_data = param_data
            logname = "User_Data_Server"
            log_msg = "[User_Data_Server][WriteUserParams]user_params yaml_data is none!" # 正常情况下yaml_data的值不为空
            log.LogInfo(logname,log_msg)
        if not yaml_data:
            logname = "User_Data_Server"
            log_msg = "[User_Data_Server][WriteUserParams]user_params yaml_data is none!" # 正常情况下yaml_data的值不为空
            log.LogWarn(logname,log_msg)
            return
        yaml_w = yaml.YAML()
        yaml_w.indent(sequence=4, offset=2)
        with open(self.__user_params_path,'w') as f: 
            yaml_w.dump(yaml_data,f)
        os.system("sync")


    #---------------------------------------#
    #******#  外设记忆功能参数文件操作 #******# 
    #---------------------------------------#

    def LoadCleandeviceWithMemoryParams(self):
        '''
            加载外设记忆功能参数
        '''
        os.system("rosparam load {}".format(self.__cleandevice_with_memory_params_path))
    
    def ReadCleandeviceWithMemoryParams(self):
        '''
            读取参数
        '''
        yaml_data={}
        if os.path.exists(self.__cleandevice_with_memory_params_path):
            with open(self.__cleandevice_with_memory_params_path,'r') as f: 
                yaml_data = yaml.load(f,Loader=yaml.Loader)
        return yaml_data
    
    def WriteCleandeviceWithMemoryParams(self,param_data):
        '''
            写入参数
            - param_data (dict)
        '''
        yaml_data = self.ReadCleandeviceWithMemoryParams()
        if yaml_data : 
            yaml_data.update(param_data)
        else: 
            yaml_data = param_data
        yaml_w = yaml.YAML()
        yaml_w.indent(sequence=4, offset=2)
        with open(self.__cleandevice_with_memory_params_path,'w') as f: 
            yaml_w.dump(yaml_data,f)
        os.system("sync")

    def DeleteCleandeviceWithMemoryParams(self,param_data):
        '''
            删除参数
            - param_data (dict)
        '''
        yaml_data = param_data
        yaml_w = yaml.YAML()
        yaml_w.indent(sequence=4, offset=2)
        with open(self.__cleandevice_with_memory_params_path,'w') as f: 
            yaml_w.dump(yaml_data,f)
    

    #---------------------------------------#
    #******# 地图旋转角度临时存储文件 #******# 
    #---------------------------------------#

    def ReadMapRotateAngleParams(self):
        '''
            读取参数
        '''
        yaml_data={}
        if os.path.exists(self.__map_rotate_angle_info_path):
            with open(self.__map_rotate_angle_info_path,'r') as f: 
                yaml_data = yaml.load(f,Loader=yaml.Loader)
        return yaml_data
    
    def WriteMapRotateAngleParams(self,param_data):
        '''
            写入参数
            - param_data (dict)
        '''
        if not os.path.exists(self.__map_rotate_angle_info_path):
            ret = os.system("touch {}".format(self.__map_rotate_angle_info_path))
        yaml_data = param_data
        yaml_w = yaml.YAML()
        yaml_w.indent(sequence=4, offset=2)
        with open(self.__map_rotate_angle_info_path,'w') as f: 
            yaml_w.dump(yaml_data,f)
        os.system("sync")


    #---------------------------------------#
    #******# 新外设参数记忆文件 20230906 #******# 
    #---------------------------------------#

    def ReadNewCleanDeviceParams(self):
        '''
            读取参数
        '''
        yaml_data={}
        if os.path.exists(self.__new_cleandevice_path):
            with open(self.__new_cleandevice_path,'r') as f: 
                yaml_data = yaml.load(f,Loader=yaml.Loader)
        return yaml_data



    def WriteNewCleanDeviceParams(self,param_data):
        '''
            写入参数
            - param_data (dict)
        '''
        if not os.path.exists(self.__new_cleandevice_path):
            ret = os.system("touch {}".format(self.__new_cleandevice_path))
        yaml_data = param_data
        yaml_w = yaml.YAML()
        yaml_w.indent(sequence=4, offset=2)
        with open(self.__new_cleandevice_path,'w') as f: 
            yaml_w.dump(yaml_data,f)
        os.system("sync")



    #---------------------------------------#
    #******#  消杀功能参数文件操作 #******# 
    #---------------------------------------#

    def LoadDisinfectConfigParams(self):
        '''
            加载消杀功能参数
        '''
        os.system("rosparam load {}".format(self.__disinfect_config_path))
    
    def ReadDisinfectConfigParams(self):
        '''
            读取参数
        '''
        yaml_data={}
        if os.path.exists(self.__disinfect_config_path):
            with open(self.__disinfect_config_path,'r') as f: 
                yaml_data = yaml.load(f,Loader=yaml.Loader)
        return yaml_data
    
    def WriteDisinfectConfigParams(self,param_data):
        '''
            写入参数
            - param_data (dict)
        '''
        yaml_data = self.ReadDisinfectConfigParams()
        if yaml_data : 
            yaml_data.update(param_data)
        else: 
            yaml_data = param_data
        yaml_w = yaml.YAML()
        yaml_w.indent(sequence=4, offset=2)
        with open(self.__disinfect_config_path,'w') as f: 
            yaml_w.dump(yaml_data,f)
        os.system("sync")


    #---------------------------------------#
    #******#  机器人支持功能参数文件操作 #******# 
    #---------------------------------------#
    
    def ReadRobotFunctionParams(self):
        '''
            读取参数
        '''
        yaml_data={}
        if os.path.exists(self.__robot_funtion_path):
            with open(self.__robot_funtion_path,'r') as f: 
                yaml_data = yaml.load(f,Loader=yaml.Loader)
        return yaml_data

    #---------------------------------------#
    #******# 传感器禁用参数文件操作 #******# 
    #---------------------------------------#

    def ReadSensorDisableInfo(self):
        '''
            读取传感器信息
        '''
        yaml_data = {}
        
        
        # 文件存在读取内容
        # if os.path.exists(self.__sensor_disable_info_path):
        #     with open(self.__sensor_disable_info_path,'r') as f: 
        #         yaml_data = yaml.load(f,Loader=yaml.Loader)
        
        
        #print(yaml_data)
        # 值为空，创建传感器禁用列表
        # if not yaml_data : 
        #     yaml_data = self.GetDefaultSensorDisableInfo()
        #     #print(yaml_data)
                
        yaml_data = self.GetDefaultSensorDisableInfo() # 修改20240123-dw.liu V3.2.0将弃用这个文件
        return yaml_data

    def WriteSensorDisableInfoParams(self,param_data):
        '''
            写入参数
            - param_data (列表)
        '''
         
        yaml_data = param_data
        yaml_w = yaml.YAML()
        yaml_w.indent(sequence=4, offset=2)
        with open(self.__sensor_disable_info_path,'w') as f: 
            yaml_w.dump(yaml_data,f)
        os.system("sync")


    #---------------------------------------#
    #******# 机器人编码和型号参数文件读取操作 #******# 
    #---------------------------------------#
    def ReadRobotSnParams(self):
        '''
            读取参数
        '''
        yaml_data={}
        if os.path.exists(self.__robot_sn_path):
            with open(self.__robot_sn_path,'r') as f: 
                yaml_data = yaml.load(f,Loader=yaml.Loader)
        return yaml_data
        
        



    def ReadDustboxTime(self):
        '''
            读取参数尘盒提醒时间
        '''
        if not os.path.exists(self.__dustbox_time_path):
            self.WriteDustboxTime()
        read_dictionary = np.load(self.__dustbox_time_path).item()
        read_dictionary["robot_curtime"] = int(time.time())
        return read_dictionary
    
    def WriteDustboxTime(self):
        '''
            保存参数尘盒提醒时间
        '''
        data = {
                "dustbox_cleantime": int(time.time())
            }
        np.save(self.__dustbox_time_path, data)

    def GenerateSensorInfo(self, id, type, name, name_ch, used = 1):
        '''
            生成传感器dict
        '''
        return {"id": id,"type": type, "name":name, "name_ch":name_ch,"used": used}
    
    def GenerateSensorDisableInfo(self, id, type, name, name_ch, used = 4,enable = 0):
        '''
            生成传感器禁用dict (disable_),默认全部禁止禁用
        '''
        return {"id": id,"type": type, "name":name, "name_ch":name_ch,"used": used,"enable": enable}

    def GenerateDeviceInfo(self, id, type, name, name_ch, used = 1):
        '''
            生成外设dict
        '''
        return {"id": id, "name":name,"type": type, "name_ch":name_ch, "used": used} 

    def GetDefaultSensorInfo(self):
        '''
            获取默认传感器信息
        '''
        data = []
        data.append(self.GenerateSensorInfo(SensorType.BUMPER_1,SensorType.BUMPER,"bumper_1","碰撞传感器1"))
        data.append(self.GenerateSensorInfo(SensorType.BUMPER_2,SensorType.BUMPER,"bumper_2","碰撞传感器2"))
        data.append(self.GenerateSensorInfo(SensorType.BUMPER_3,SensorType.BUMPER,"bumper_3","碰撞传感器3"))
        data.append(self.GenerateSensorInfo(SensorType.BUMPER_4,SensorType.BUMPER,"bumper_4","碰撞传感器4"))

        data.append(self.GenerateSensorInfo(SensorType.DROP_1,SensorType.DROP,"drop_1","跌落传感器1"))
        data.append(self.GenerateSensorInfo(SensorType.DROP_2,SensorType.DROP,"drop_2","跌落传感器2"))
        data.append(self.GenerateSensorInfo(SensorType.DROP_3,SensorType.DROP,"drop_3","跌落传感器3"))
        data.append(self.GenerateSensorInfo(SensorType.DROP_4,SensorType.DROP,"drop_4","跌落传感器4"))

        data.append(self.GenerateSensorInfo(SensorType.SONAR_1,SensorType.SONAR,"sonar_1","超声波传感器1"))
        data.append(self.GenerateSensorInfo(SensorType.SONAR_2,SensorType.SONAR,"sonar_2","超声波传感器2"))
        data.append(self.GenerateSensorInfo(SensorType.SONAR_3,SensorType.SONAR,"sonar_3","超声波传感器3"))
        data.append(self.GenerateSensorInfo(SensorType.SONAR_4,SensorType.SONAR,"sonar_4","超声波传感器4"))

        return data
    
    def GetDefaultSensorDisableInfo(self):
        '''
            获取默认传感器禁用信息
        '''
        data = []
        data.append(self.GenerateSensorDisableInfo("disable_left_solid_laser",SensorType.UNKNOWN,"disable_left_solid_laser","左侧固态雷达"))
        data.append(self.GenerateSensorDisableInfo("disable_right_solid_laser",SensorType.UNKNOWN,"disable_right_solid_laser","右侧固态雷达"))
        data.append(self.GenerateSensorDisableInfo("disable_back_solid_laser",SensorType.UNKNOWN,"disable_back_solid_laser","后侧固态雷达"))
        # data.append(self.GenerateSensorDisableInfo("disable_depth_camera",SensorType.UNKNOWN,"disable_depth_camera","深度相机"))
        data.append(self.GenerateSensorDisableInfo("disable_depth_camera_down",SensorType.UNKNOWN,"disable_depth_camera_down","深度相机 下部"))
        data.append(self.GenerateSensorDisableInfo("disable_depth_camera_up",SensorType.UNKNOWN,"disable_depth_camera_up","深度相机 上部"))
        data.append(self.GenerateSensorDisableInfo("disable_sonar",SensorType.UNKNOWN,"disable_sonar","超声波"))
        #print(data)
        

        return data

    def GetDefaultDeviceInfo(self):
        '''
            获取默认设备信息
        '''
        data = []

        data.append(self.GenerateDeviceInfo(CleanDeviceType.MAIN_BRUSH,self.__CONTROL_SPPEED_TYPE,"main_brush","主刷"))
        data.append(self.GenerateDeviceInfo(CleanDeviceType.SIDEBRUSH_LF,self.__CONTROL_OPEN_TYPE,"sidebrush_lf","前侧左边刷"))
        data.append(self.GenerateDeviceInfo(CleanDeviceType.SIDEBRUSH_RF,self.__CONTROL_OPEN_TYPE,"sidebrush_rf","前侧右边刷"))
        #data.append(self.GenerateDeviceInfo(CleanDeviceType.SIDEBRUSH_LB,"sidebrush_lb","后侧左边刷"))
        #data.append(self.GenerateDeviceInfo(CleanDeviceType.SIDEBRUSH_RB,"sidebrush_rb","后侧右边刷"))

        data.append(self.GenerateDeviceInfo(CleanDeviceType.LIFT_MAINBRUSH,self.__CONTROL_LIFT_TYPE,"lift_mainbrush","主刷升降"))
        data.append(self.GenerateDeviceInfo(CleanDeviceType.LIFT_LF,self.__CONTROL_LIFT_TYPE,"lift_lf","左边刷升降"))
        data.append(self.GenerateDeviceInfo(CleanDeviceType.LIFT_RF,self.__CONTROL_LIFT_TYPE,"lift_rf","右边刷升降"))

        data.append(self.GenerateDeviceInfo(CleanDeviceType.VACUUMFAN,self.__CONTROL_SPPEED_TYPE,"vacuumfan","真空风扇"))
        data.append(self.GenerateDeviceInfo(CleanDeviceType.VACUUMFAN_2,self.__CONTROL_SPPEED_TYPE,"vacuumfan_2","真空风扇2"))

        data.append(self.GenerateDeviceInfo(CleanDeviceType.DISINFECT,self.__CONTROL_OPEN_TYPE,"disinfect","消毒喷雾器"))
        # data.append(self.GenerateDeviceInfo(CleanDeviceType.DISINFECT_2,self.__CONTROL_OPEN_TYPE,"disinfect_2","消毒喷雾器2"))

        data.append(self.GenerateDeviceInfo(CleanDeviceType.ABSORBENT_MAIN,self.__CONTROL_OPEN_TYPE,"main_absorbent","吸水扒"))
        data.append(self.GenerateDeviceInfo(CleanDeviceType.ABSORBENT_SB_LF,self.__CONTROL_OPEN_TYPE,"absorbent_lf","左前吸水扒"))
        data.append(self.GenerateDeviceInfo(CleanDeviceType.ABSORBENT_SB_RF,self.__CONTROL_OPEN_TYPE,"absorbent_rf","右前吸水扒"))

        data.append(self.GenerateDeviceInfo(CleanDeviceType.BOX_RUBBISH,self.__NO_CONTROL_TYPE,"box_rubbish","垃圾盒"))
        data.append(self.GenerateDeviceInfo(CleanDeviceType.BOX_DUST,self.__NO_CONTROL_TYPE,"box_dust","尘盒"))
        data.append(self.GenerateDeviceInfo(CleanDeviceType.BOX_CLEANWATER,self.__NO_CONTROL_TYPE,"box_cw","清水箱"))
        data.append(self.GenerateDeviceInfo(CleanDeviceType.BOX_DIRTYWATER,self.__NO_CONTROL_TYPE,"box_dw","污水箱"))

        data.append(self.GenerateDeviceInfo(CleanDeviceType.HAND_RAIL,self.__CONTROL_OPEN_TYPE,"hand_rail","扶手"))

        data.append(self.GenerateDeviceInfo(CleanDeviceType.DISINFECT_L,self.__CONTROL_OPEN_TYPE,"disinfect_l","消毒喷雾左侧"))
        data.append(self.GenerateDeviceInfo(CleanDeviceType.DISINFECT_R,self.__CONTROL_OPEN_TYPE,"disinfect_r","消毒喷雾右侧"))

        data.append(self.GenerateDeviceInfo(CleanDeviceType.CARPET_MODE,self.__CONTROL_OPEN_TYPE,"carpet_mode","地毯模式"))
        return data
    
    def GetDefaultCleanDeviceWithMemoryInfo(self):
        '''
            获取默认外设记忆模式
        '''
        data = {
            "default_memory_name": "mode1679031729",        # 默认模式名      
            "mode1679031729" : {                            # 机器内部定义的模式名："mode" + "创建时间戳"
                                "id_22": 32,                # 主刷升降     下降
                                "id_203": 8,                # 地毯模式      关闭
                                "item_name": "默认模式",     # 用户定义的模式名
                                "id_20": 32,                 # 前侧左边刷升降   下降
                                "id_31": 22,                 # 真空风扇速度     中
                                "id_10": 22,                 # 主刷速度         中
                                "id_11": 21,                 # 前侧左边刷开关   低
                                "id_12": 21,                 # 前侧右边刷开关   低
                                "id_21": 32,                  # 前侧右边刷升降   下降
                                "id_203_data":0                 # 地毯模式，用户自定义值，默认0
                                }
                }
        
        return data
    
    def GetDefaultNewDeviceInfo(self):
        '''
        获取默认外设记忆模式(新的文件格式)20231123-dw.liu
        '''
        data = {
            "默认模式" : {                            
                                "is_defualt": 1,
                                "id_22": 32,                # 主刷升降     下降
                                "id_203": 8,                # 地毯模式      关闭
                                "item_name": "默认模式",     # 用户定义的模式名
                                "id_20": 32,                 # 前侧左边刷升降   下降
                                "id_31": 22,                 # 真空风扇速度     中
                                "id_10": 22,                 # 主刷速度         中
                                "id_11": 21,                 # 前侧左边刷开关   低
                                "id_12": 21,                 # 前侧右边刷开关   低
                                "id_21": 32,                  # 前侧右边刷升降   下降
                                "id_203_data":0                 # 地毯模式，用户自定义值，默认0
                                # "id_41":8 #消杀模式
                                # "id_42":8 # 消毒喷雾左侧
                                # "id_43":8 # 消毒喷雾右侧
                                }
                }
        
        return data
    
    def GetDefaultDisinfectConfigInfo(self):
        '''
            获取默认消杀
        '''
        data = {
            "spray_mode": "Continuous",                             # Continuous 持续模式  Intermit  间歇模式    
            "disinfect_l" : {                                                            # 左侧雾箱
                                "liquid_total": 110,                               # 雾箱总容量
                                "liquid_hight_level": 90,                  # 雾箱高液位值
                                "liquid_low_level": 20,                        # 雾箱低液位值
                                "liquid_capacity":4,                        # 单位 l
                                "spary_open_time":1,                       # 间歇模式喷射时间
                                "spary_sleep_time": 1,                     # 间歇模式休眠时间
                                "enable": True                                             # 雾箱使能状态
                                },
            "disinfect_r" : {                            
                                "liquid_total": 110,                
                                "liquid_hight_level": 90,                
                                "liquid_low_level": 20,
                                "liquid_capacity":4,                        # 单位 l     
                                "spary_open_time":1,
                                "spary_sleep_time": 1,                 
                                "enable": True           
                                }
                }
        
        return data
    
    def GetRobotFuntionInfo(self):
        '''
            获取默认机器人支持的新功能
            从门禁、地图更新开始
        '''
        data = {
            "access_control": 0,                             # 门禁 1支持 0或空不支持
            "map_update":0                                  # 地图更新 1支持 0或空不支持
                }
        
        return data
    
    
    def ReadSensorInfo(self):
        '''
            读取传感器信息
        '''
        yaml_data = {}

        if os.path.exists(self.__sensor_info_path):
            with open(self.__sensor_info_path,'r') as f: 
                yaml_data = yaml.load(f,Loader=yaml.Loader)

        if yaml_data is None: 
            yaml_data = self.GetDefaultSensorInfo()
        
        for item in yaml_data:
            if item.has_key("used") and (item["used"] == 1):
                self.__used_sensor_info.append(item)
                self.__used_sensor_set.add(item["id"])

    def ReadDeviceInfo(self):
        '''
            读取外设信息
        '''
        yaml_data = {}

        if os.path.exists(self.__device_info_path):
            with open(self.__device_info_path,'r') as f: 
                yaml_data = yaml.load(f,Loader=yaml.Loader)

        if yaml_data is None: 
            yaml_data = self.GetDefaultDeviceInfo()

        for item in yaml_data:
            if item.has_key("used") and (item["used"] == 1):
                self.__used_device_info.append(item)
                self.__used_device_set.add(item["id"])

    def WriteDefaultInfo(self, path, data):
        '''
            写入默认信息
        '''
        yaml_w = yaml.YAML()
        yaml_w.indent(sequence=4, offset=2)
        with open(path,'w') as f: 
            yaml_w.dump(data,f)
            f.close()

    def GetSensorInfo(self):
        '''
            获取传感器信息
        '''
        return self.__used_sensor_info
    
    def GetDeviceInfo(self):
        '''
            获取外设信息
        '''
        return self.__used_device_info

    def GetUsedSensorSet(self):
        '''
            获取使用的传感器集
        '''
        return self.__used_sensor_set

    def GetUsedDeviceSet(self):
        '''
            获取使用的外设集
        '''
        return self.__used_device_set

    def SetUsedSensorSet(self, sensor_set):
        '''
            设置使用的传感器集
        '''
        self.__used_sensor_set = sensor_set

    def SetUsedDeviceSet(self, device_set):
        '''
            设置使用的外设集
        '''
        self.__used_device_set = device_set

    def IsUsedSensor(self, sensor_id):
        '''
            传感器是否使用
        '''
        return sensor_id in self.__used_sensor_set

    def IsUsedDevice(self, device_id):
        '''
            外设是否使用
        '''
        return device_id in self.__used_device_set

    def GetVervionInfo(self):
        '''
        获取版本信息
        '''
        yaml_data = {}

        if os.path.exists(self.__version_info_path):
            with open(self.__version_info_path,'r') as f: 
                yaml_data = yaml.load(f,Loader=yaml.Loader)

        if yaml_data.has_key("version"): 
            return yaml_data["version"]
        else:
            return ""
